package com.zmn.plat.business.impl.category;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.plat.business.interfaces.category.BaseCategoryAttributeBService;
import com.zmn.plat.model.dto.BaseCategoryAttributeDTO;
import com.zmn.plat.model.entity.category.BaseCategoryAttribute;
import com.zmn.plat.model.entity.category.BaseCategoryAttributeEnum;
import com.zmn.plat.services.interfaces.category.BaseCategoryAttributeEnumService;
import com.zmn.plat.services.interfaces.category.BaseCategoryAttributeService;

/**
 * @author duanzuocai
 * @version v1.0 @类说明：
 * @since 2018/10/19 20:52
 */
@Service
public class BaseCategoryAttributeBServiceImpl implements BaseCategoryAttributeBService {
    @Resource
    BaseCategoryAttributeService attributeService;
    @Resource
    BaseCategoryAttributeEnumService attributeValueService;

    @Override
    public List<BaseCategoryAttributeDTO> getAttrAndEnumByCategoryId(Integer categoryId) {
        List<BaseCategoryAttribute> attrs = attributeService.listByCategoryId(categoryId, null);

        List<BaseCategoryAttributeEnum> values = attributeValueService.listByCategoryId(categoryId);

        java.util.Map<Integer, BaseCategoryAttributeDTO> map = new HashMap<>();

        for (BaseCategoryAttribute attr : attrs) {
            BaseCategoryAttributeDTO view = new BaseCategoryAttributeDTO();
            view.setAttrId(attr.getAttrId());
            view.setName(attr.getName());
            view.setCategId(attr.getCategId());
            view.setStatus(attr.getStatus());
            map.put(view.getAttrId(), view);
        }
        for (BaseCategoryAttributeEnum value : values) {
            int attrid = value.getAttrId();
            BaseCategoryAttributeDTO view = map.get(attrid);
            if (view == null) {
                continue;
            }
            List<BaseCategoryAttributeEnum> enums = view.getAttributeEnums();
            if (enums == null) {
                enums = new ArrayList<>();
                view.setAttributeEnums(enums);
            }

            enums.add(value);
        }

        List<BaseCategoryAttributeDTO> list = new ArrayList<BaseCategoryAttributeDTO>();

        Collection<BaseCategoryAttributeDTO> collection = map.values();

        list.addAll(collection);
        return list;
    }
}
